package com.tupu.controller.dispatch;

import com.tupu.common.PageResult;
import com.tupu.common.Result;
import com.tupu.entity.dispatch.PlanText;
import com.tupu.entity.dispatch.WarningEventFailurePlan;
import com.tupu.entity.dto.WarningEventFailurePlanDto;
import com.tupu.service.dispatch.PlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 预案管理
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/plan")
public class PlanController {

    @Autowired(required = false)
    private PlanService planService;

    /**
     * 条件查询预案管理列表
     *
     * @param pageNum
     * @param pageSize
     * @param planName
     * @param device
     * @param catygory (category前端命名错误)1:站点；2：母线3：线路
     * @param level    ( 1:一级；2：二级；3：三级；4：四级；5：五级)
     * @return
     */
    @PostMapping("/planList")
    public Result planList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false) String planName,
                           @RequestParam(required = false) String device,
                           @RequestParam(required = false) String station,
                           @RequestParam(required = false) Integer catygory,
                           @RequestParam(required = false) Integer level) {
        try {
            PageResult<WarningEventFailurePlanDto> info = planService.conditionSearch(pageNum, pageSize, planName, device, station, catygory, level);
            return Result.ok(info);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/import", name = "故障预案上传")
    public Result importWord(@RequestParam(required = false) MultipartFile file) {
        try {
            planService.importWord(file);
            return Result.ok("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.errorMessage("错误信息：" + e);
        }
    }

    @PostMapping(value = "/planUpdate", name = "根据id编辑原有预案")
    public Result planUpdate(@RequestBody WarningEventFailurePlan plan) {
        try {
            planService.updatePlanById(plan);
            return Result.ok("编辑成功");
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    @PostMapping(value = "/planOneUpdate", name = "编辑保存预案")
    public Result planOneUpdate(@RequestBody WarningEventFailurePlan plan) {
        try {
            planService.insertPlan(plan);
            return Result.ok("编辑成功");
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 删除预案
     *
     * @param planId
     * @return
     */
    @PostMapping("/planDelete")
    public Result planDelete(@RequestParam(required = false) String planId) {
        try {
            planService.del(planId);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }

    }

    @PostMapping(value = "/planUpload", name = "下载一个预案 word")
    public Result planUpload(@RequestParam Integer planId, HttpServletResponse response) {
        try {
            planService.downloadWordByPlanId(planId, response);
            return Result.ok();
        } catch (IOException e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    @PostMapping(value = "/downloadTemplate", name = "下载预案模板，返回文件流")
    public Result downloadTemplate(HttpServletResponse response) {
        try {
            planService.downloadWordTemplate(response);
            return Result.ok();
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/planDetail", name = "查询一条预案")
    public Result planDetailById(Integer planId) {
        try {
            WarningEventFailurePlan plan = planService.findPlanByPlanId(planId);
            return Result.ok(plan);
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/copyPlan", name = "复制一条预案")
    public Result copyPlan(@RequestParam(required = false) Integer planId) {
        try {
            planService.copyPlanByPlanId(planId);
            return Result.ok("复制成功");
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

    @PostMapping(value = "/textReplace", name = "预案中的文本替换")
    public Result textReplace(@RequestBody PlanText text) {
        try {
            boolean result = planService.textReplace(text);
            if (result) {
                return Result.ok("替换成功");
            } else {
                return Result.ok("原内容不存在");
            }
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }

    @PostMapping(value = "/testAddStation", name = "预案识别新增厂站")
    public Result testAddStation() {
        try {
            planService.testAddStation();
            return Result.ok();
        } catch (Exception e) {
            return Result.errorMessage(e.toString());
        }
    }

}
